package com.txd.mofa.login;


import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.kongzue.baseframework.BaseFragment;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.Preferences;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.MainActivity;
import com.txd.mofa.R;

import java.util.Map;

import static com.txd.mofa.http.HttpUtils.advertOfLogin;


/**
 * A simple {@link Fragment} subclass.
 */

@Layout(R.layout.fgt_boot_page)
public class BootPageFgt extends BaseFragment {
    private ImageView rl_view;
    private int[] bgRes = {R.mipmap.beij2, R.mipmap.beij3,R.mipmap.beij4};
    private Map<String, String> data;
    private TextView tv;

    @Override
    public void initViews() {
        rl_view = findViewById(R.id.rl_view);
        tv = findViewById(R.id.tv);
    }

    private int index;
    private int anInt;

    @Override
    public void initDatas() {
        index = getArguments().getInt("index");
        rl_view.setImageResource(bgRes[index]);
//        index == 2 ? View.VISIBLE : View.GONE

        if (index == 2){
            tv.setVisibility(View.VISIBLE);
        }else {
            tv.setVisibility(View.GONE);
        }
        http();

        anInt = Preferences.getInstance().getInt(me, "user2", "bootpage");
        Preferences.getInstance().set(me, "user2", "bootpage", ++anInt);
    }

    @Override
    public void setEvents() {
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (index == 2) {
                    // 第一次进入应用  进入引导页  第二次进入  进入 登录界面   判断是否登录  或者判断数据库中是否有数据  然后直接进入主界面
                    if (anInt == 1) {
                        jump(BootPageAty.class);
                    } else if (anInt >= 1) {
                        Preferences.getInstance().set(me,"url","url",data.get("img_url"));
                        Preferences.getInstance().set(me,"zhi","zhi",data.get("value"));
                        Preferences.getInstance().set(me,"id","id",data.get("id"));
                        jump(QidongyeAty.class);
                        getActivity().finish();
                    }
                }
            }
        });
    }


    private void http() {
        HttpRequest.POST(me, advertOfLogin, new Parameter(), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")) {
                        data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                    }

                } else {
                    Toast.makeText(me, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
